package main;

import lightObjects.AreaLight;
import lightObjects.DirectLight;
import lightObjects.PointLight;
import sceneObjects.Box;
import sceneObjects.CircularPlane;
import sceneObjects.Cylinder;
import sceneObjects.RectPlane;
import sceneObjects.Sphere;
import sceneObjects.TriangularMeshes;

public class SceneParser extends Parser {
    Scene scene;

    private Parseable curObject;

    public SceneParser(Scene scene) {
	this.scene = scene;
    }

    public void startFile() {
	System.out.println("----------------");
    }

    public void endFile() throws ParseException {
	scene.finalizeObject();
    }

    // start a new object definition
    // return true if recognized
    public boolean addObject(String name) throws ParseException {
	if (name.equals("scene")) {
	    curObject = scene;
	} else if (name.equals("camera")) {
	    curObject = new Camera();
	} else if (name.equals("rectangle")) {
	    curObject = new RectPlane();
	} else if (name.equals("box")) {
	    curObject = new Box();
	} else if (name.equals("disc")) {
	    curObject = new CircularPlane();
	} else if (name.equals("sphere")) {
	    curObject = new Sphere();
	} else if (name.equals("mesh")) {
	    curObject = new TriangularMeshes();
	} else if (name.equals("cylinder")) {
	    curObject = new Cylinder();
	} else if (name.equals("light-directed")) {
	    curObject = new DirectLight();
	} else if (name.equals("light-point")) {
	    curObject = new PointLight();
	} else if (name.equals("light-area")) {
	    curObject = new AreaLight();
	} else {
	    return false;
	}
	System.out.println("OBJECT: " + name);
	return true;
    }

    // set a specific parameter for the current object
    // return true if recognized
    public boolean setParameter(String name, String[] args) throws ParseException {
	return curObject.setParameter(name, args);
    }

    // finish the parsing of the current object
    // here is the place to perform any validations over the parameters and
    // final initializations.
    public void commit() throws ParseException {
	if (curObject != scene) {
	    if (curObject.finalizeObject()) {
		scene.add(curObject);
	    } else {
		System.out.println("Error: Parsing object failed, trying to skip.");
	    }
	}
    }

    public void reportError(String err) {
	System.out.println("ERROR: " + err);
    }

}
